// pages/editstudent/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { name: '男', value: '0', checked: 'true' },
      { name: '女', value: '1' }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  formSubmit: function (e) {    
    var bool = true;
    if (!e.detail.value.school) {
      bool = false;
      wx.showToast({
        title: '在读学校名称不能为空',
        icon: 'none',
        duration: 2000
      })
    }
    if (!e.detail.value.phone) {
      bool = false;
      wx.showToast({
        icon: 'none',
        title: '联系电话不能为空',
        duration: 2000
      })
    }else if(e.detail.value.phone.length < 11){
      bool = false;
      wx.showToast({
        icon: 'none',
        title: '联系电话格式不正确',
        duration: 2000
      })
    }    
    if(!e.detail.value.name){
      bool = false;
      wx.showToast({
        icon: 'none',
        title: '学员姓名不能为空',
        duration: 2000
      })
    }    
    if(bool){
      wx.showModal({
        title: '提示',
        content: '确定保存学员信息吗？',
        success: res => {
          if (res.confirm) {
            //获取热门课程
            wx.request({
              url: 'https://sng.tsletu.com/api/wxapp/user/addUserStudents',
              data: {
                'name': e.detail.value.name,
                'phone': e.detail.value.phone,
                'current_school': e.detail.value.school,
                'certificate_no': e.detail.value.certificate,
                'sex': e.detail.value.sex
              },
              header: {
                'XX-Device-Type': 'wxapp',
                'XX-Token': app.globalData.token
              },
              success: res => {                
                if(res.data.code == 1){
                  wx.showToast({
                    icon: 'none',
                    title: '保存成功!',
                    duration: 2000
                  })                  
                  wx.navigateBack({
                    delta: 1
                  })
                }else{
                  wx.showToast({
                    icon: 'none',
                    title: '保存失败！',
                    duration: 2000
                  })
                }
              }
            })
          }
        }
      }) 
    }       
  }  
})